/**
 * Created by Arnald on 2017/5/3.
 */
const app = require('express')();
const variable = require('../config/variable.js');
const mem = require('../util/mem.js');
const request = require('../util/request.js');
const bluedSDK = require('../util/bluedSDK.js');
const gameUser = require('./gameUser.js');

// 建立一个创建房间create
app.get('/create', function (req, res) {
    var result = {};
    if (!req.query.data) {
        result.code = 101;
        res.send(result).end();
        return;
    }

    var data = JSON.parse(req.query.data);
    var user = data.user;
    var lv = data.lv;
    var inGame = false;
    for (var i in user) {
        if (mem.gamePlayers[user[i].id]) {
            inGame = true;
            if (!result.inGameId) {
                result.inGameId = {};
            }
            result.inGameId[user[i].id] = {};
            result.inGameId[user[i].id].local = mem.gamePlayers[user[i].id].local;
            result.inGameId[user[i].id].gameId = mem.gamePlayers[user[i].id].gameId;
            result.inGameId[user[i].id].gameType = variable.gameLv[mem.games[mem.gamePlayers[user[i].id].gameId].lv].type;
        }
    }
    if (inGame) {
        result.code = 102;
        res.send(result).end();
        return;
    }

    var gameId;
    while (true) {
        var key = Math.floor(Math.random() * 10000000);
        if (!mem.games[key]) {
            gameId = key;
            mem.games[key] = {};
            break;
        }
    }

    var ids = [];
    var player = {};
    for (var i in user) {
        setPlayer(user[i], gameId, i);
        if (user[i].robot) {
            mem.gamePlayers[user[i].id].robot = true;
            mem.gamePlayers[user[i].id].line = 'on';
            mem.gamePlayers[user[i].id].inRoom = true;
            mem.gamePlayers[user[i].id].ready = true;
        }
        ids.push(user[i].id);
        player[user[i].id] = i;
    }

    setGame(gameId, ids, lv, 'match');

    var param = {};
    param.gameId = gameId;
    param.lv = lv;
    request.addGame(param, function () {
    });

    result.code = 200;
    result.key = gameId;
    result.player = player;
    res.send(result).end();
});

app.get('/query', function (req, res) {
    var result = {};
    if (!req.query.id) {
        result.code = 101;
    } else if (mem.gamePlayers[req.query.id]) {
        result.code = 102;
        result.gameId = mem.gamePlayers[req.query.id].gameId;
        result.local = mem.gamePlayers[req.query.id].local;
        //result.gameType = variable.gameLv[mem.games[mem.gamePlayers[req.query.id].gameId].lv].type;
    } else {
        result.code = 200;
    }
    res.send(result).end();
});

app.get('/custom', function (req, res) {
    var result = {};
    console.log(req.query)
    if (!req.query.id) {
        result.code = 101;
        res.send(result).end();
        return;
    }
    if (mem.gamePlayers[req.query.id]) {
        result.code = 102;
        result.gameId = mem.gamePlayers[req.query.id].gameId;
        result.local = mem.gamePlayers[req.query.id].local;
        //result.gameType = variable.gameLv[mem.games[mem.gamePlayers[req.query.id].gameId].lv].type;
        res.send(result).end();
        return;
    }

    var id = req.query.id;
    var password = (!req.query.password) ? req.query.password : null;
    var rounds = (!req.query.rounds) ? req.query.rounds : 6;
    var gameId;
    while (true) {
        var key = Math.floor(Math.random() * 10000000);
        if (!mem.games[key]) {
            gameId = key;
            break;
        }
    }

    setPlayer(req.query, gameId, 0);

    setGame(key, [id], 0, 'custom');
    mem.games[key].password = password;
    mem.games[key].rounds = rounds;

    result.key = gameId;
    result.local = 0;
    result.code = 200;
    res.send(result).end();
});
app.get('/follow', function (req, res) {
    var result = {};
    if (!req.query.id && !req.query.gameId) {
        result.code = 101;
        res.send(result).end();
        return;
    }
    if (mem.gamePlayers[req.query.id]) {
        result.code = 102;
        result.gameId = mem.gamePlayers[req.query.id].gameId;
        result.local = mem.gamePlayers[req.query.id].local;
        result.gameType = variable.gameLv[mem.games[mem.gamePlayers[req.query.id].gameId].lv].type;
        res.send(result).end();
        return;
    }
    if (!mem.games[req.query.gameId]) {
        result.code = 103;
        res.send(result).end();
        return;
    }
    if (mem.games[req.query.gameId].id.length >= 3) {
        result.code = 104;
        res.send(result).end();
        return;
    }
    if (mem.games[req.query.gameId].password && mem.games[req.query.gameId].password != req.query.password) {
        result.code = 105;
        res.send(result).end();
        return;
    }

    var key = req.query.gameId;
    var id = req.query.id;
    var local = mem.games[key].id.length;

    setPlayer(req.query, gameId, local);

    mem.games[key].id.push(id);

    result.key = key;
    result.local = local;
    result.code = 200;
    res.send(result).end();
});
app.get('/roomList', function (req, res) {
    var result = {};
    if (!req.query.id) {
        result.code = 101;
        res.send(result).end();
        return;
    }
    if (!req.query.bluedId) {
        result.code = 102;
        res.send(result).end();
        return;
    }
    var param = {};
    param.uid = req.query.bluedId;
    request.getBlued(param, function (res2) {
        bluedSDK.friends(res2.accessToken, req.query.bluedId, function (res3) {
            var friendUid = [];
            if (res3.data) {
                for (var i in res3.data) {
                    friendUid.push(res3.data[i].uid);
                }
            }
            result.code = 200;
            result.game = [];
            if (friendUid.length > 0) {
                for (var i in mem.games) {
                    var game = mem.games[i];
                    if (mem.games[i].type != 'custom') {
                        continue;
                    }
                    if (friendUid.indexOf(mem.gamePlayers[game.id[0]].bluedId) < 0) {
                        continue;
                    }
                    var data = {};
                    data.gameId = i;
                    data.mode = 1;
                    data.masterId = mem.gamePlayers[game.id[0]].id;
                    data.masterName = mem.gamePlayers[game.id[0]].name;
                    data.masterHead = mem.gamePlayers[game.id[0]].avatar;
                    //data.user = [];
                    //for (var j in game.id) {
                    //    var id = game.id[j];
                    //    data.user.push({'name': mem.gamePlayers[id].name, 'avatar': mem.gamePlayers[id].avatar});
                    //}
                    result.game.push(data);
                }
            }
            res.send(result).end();
        });
    });
});

module.exports = app;

function setGame(gameId, ids, lv, type) {
    mem.games[gameId].gameId = gameId;
    mem.games[gameId].lv = lv;
    mem.games[gameId].id = ids;
    mem.games[gameId].type = type;

    var param = {};
    param.gameId = gameId;
    param.lv = lv;
    request.addGame(param, function () {
    })
}

function setPlayer(user, gameId, local) {
    mem.gamePlayers[user.id] = {};
    mem.gamePlayers[user.id].id = user.id;
    mem.gamePlayers[user.id].cid = null;
    mem.gamePlayers[user.id].uuid = null;
    mem.gamePlayers[user.id].local = local;
    mem.gamePlayers[user.id].gameId = gameId;
    mem.gamePlayers[user.id].line = null;
    mem.gamePlayers[user.id].inRoom = false;
    mem.gamePlayers[user.id].ready = false;
    mem.gamePlayers[user.id].name = user.name;
    mem.gamePlayers[user.id].avatar = user.avatar;
    mem.gamePlayers[user.id].score = user.score;
    mem.gamePlayers[user.id].gold = user.gold;
    mem.gamePlayers[user.id].bluedId = user.bluedId;
    mem.gamePlayers[user.id].weixinId = user.weixinId;
    
    if (user.robot) {
        gameUser.setRobot(user.id, gameId);
    } else {
        gameUser.setGameId(user.id, gameId);
    }
}